﻿using System;
using System.Collections.Generic;
using System.Reflection;
using DataGeneration.Domain.Helpers;

namespace DataGeneration.Domain.CustomProcessors
{
    /// <summary>
    /// Defines the type AddressGenerator
    /// </summary>
    public class AddressGenerator
    {
        private static IList<string> _townnames;
        private static IList<string> _countrynames;
        private static readonly IList<string> StreetTokens = new List<string> { "Street", "Boulevard", "Avenue", "Drive", "crescent" };
        //private static IList<string> _houseTokens = new List<string> { "Flat", "No" };
        private static readonly IList<string> CountyTokens = new List<string> { "Renfrewshire", "Bedfordshire", "London" };
        private static readonly IList<string> AreaTokens = new List<string> { "Gorbals", "Mary Hills" };

        /// <summary>
        /// Gets the name of the country.
        /// </summary>
        /// <value>The name of the country.</value>
        public static string CountryName
        {
            get
            {
                if (_countrynames == null)
                {
                    _countrynames = FileHelper.ReadUniqueFileContent(Assembly.GetExecutingAssembly()
                                                                         .GetManifestResourceStream("DataGeneration.Domain.Resources.countrynames.txt"));
                }

                return _countrynames[RandomMizer.GetNextValue(0, _countrynames.Count - 1)];
            }
        }


        /// <summary>
        /// Gets the name of the town.
        /// </summary>
        /// <value>The name of the town.</value>
        public static string TownName
        {
            get
            {
                if (_townnames == null)
                {
                    _townnames = FileHelper.ReadUniqueFileContent(Assembly.GetExecutingAssembly()
                                                                      .GetManifestResourceStream("DataGeneration.Domain.Resources.townnames.txt"));
                }

                return _townnames[RandomMizer.GetNextValue(0, _townnames.Count - 1)];
            }
        }

        /// <summary>
        /// Gets the postcode.
        /// </summary>
        /// <value>The postcode.</value>
        public static string Postcode
        {
            get
            {
                return string.Format("{0}{1} {2}{3}", RandomMizer.RandomUpperCaseString(RandomMizer.GetNextValue(1, 2))
                                                    , RandomMizer.GetNextValue(1, 99)
                                                    , RandomMizer.GetNextValue(1, 9)
                                                    , RandomMizer.RandomUpperCaseString(2));
            }
        }

        public static string Addressline1(int maxCharacters)
        {
            return string.Format("{0} {1} {2}", GenerateHouseOrFlatNumber()
                                                    , HumanNameGenerator.HumanName
                                                    , StreetTokens[RandomMizer.GetNextValue(0, StreetTokens.Count - 1)]);
        }

        public static string Addressline2(int maxCharacters)
        {
            return AreaTokens[RandomMizer.GetNextValue(0, AreaTokens.Count - 1)];
        }

        public static string Addressline3(int maxCharacters)
        {
            return CountyTokens[RandomMizer.GetNextValue(0, CountyTokens.Count - 1)];
        }

        private static string GenerateHouseOrFlatNumber()
        {
            if (Convert.ToBoolean(RandomMizer.GetNextValue(0, 1)))
            {
                return string.Format("No {0}", RandomMizer.GetNextValue(1, 1000));
            }

            return string.Format("Flat {0} / {1}", RandomMizer.GetNextValue(1, 10), RandomMizer.GetNextValue(1, 20));
        }
    }
}